#include <time.h>
#include <tools.h>
#include <apps.h>
#include <standard/math.h>
#include <standard/version.h>
#include <standard/notification.h>
#include <standard/xml.h>
#include "bdgameeditorapp.h"
#include "mainmenu.h"
#include "maintoolbar.h"
#include "statusbar.h"
#include "workpanel.h"
#include "objectattreditorpanel.h"
#include "workcanvas.h"
#include "editorscenemgr.h"
#include "terrain/terrainobject.h"
#include "global.h"

CBDGameEditorApp::CBDGameEditorApp(){

}
CBDGameEditorApp::~CBDGameEditorApp(){

}
CUIElement* CBDGameEditorApp::Init( HUIHANDLE hParentHandle, int32 nWidth, int32 nHeight ){
    char _strBuff[64];
    char _strVersion[32];
    _SetAppcationWindowCaption( Format( _strBuff, 64, "bdgameeditor v%s", BuildAppVersion( 0, 1, _strVersion )) );

    SRand( time( NULL ) );
    SetGC( True );

    float _fLeft = 0.0f;
    float _fTop = 0.0f;
    float _fWidth = nWidth;
    float _fHeight = nHeight;
    CPanel* _pRootWnd = NEW CPanel( _fLeft, _fTop, _fWidth, _fHeight );

    _fLeft = 2.0f;
    _fTop = 2.0f;
    _fWidth = nWidth - 260;
    _fHeight = 30;
    CMainMenu* _pMainMenu = NEW CMainMenu( _fLeft, _fTop, _fWidth, _fHeight );
    _pMainMenu->Init();
    _pRootWnd->AddElement( _pMainMenu );

    _fLeft = 2.0f;
    _fTop += _fHeight - 1;
    _fWidth = nWidth - 260;
    _fHeight = 32;
    CMainToolBar* _pMainTools = NEW CMainToolBar( _fLeft, _fTop, _fWidth, _fHeight );
    _pMainTools->Init();
    _pRootWnd->AddElement( _pMainTools );

    _fLeft = 2.0f;
    _fTop += _fHeight - 1;
    _fWidth = 240;
    _fHeight = nHeight - _fTop - 30;
    CWorkPanel* _pWorkPanel = NEW CWorkPanel( _fLeft, _fTop, _fWidth, _fHeight );
    _pWorkPanel->Init();
    _pRootWnd->AddElement( _pWorkPanel );
    m_pWorkPanel = _pWorkPanel;

    _fLeft = 244 - 4;
    //_fTop += _fHeight - 1;
    _fWidth = nWidth - _fLeft - 260 + 2;
    _fHeight = nHeight - _fTop - 30;
    CWorkCanvas* _pWorkCanvas = NEW CWorkCanvas( _fLeft, _fTop, _fWidth, _fHeight );
    _pWorkCanvas->Init();
    _pRootWnd->AddElement( _pWorkCanvas );
    m_pWorkCanvas = _pWorkCanvas;

    _fLeft = nWidth - 260 + 1;
    _fTop = 2.0f;
    _fWidth = 260 - 4;
    _fHeight = nHeight - _fTop - 30;
    CObjectAttrEditorPanel* _pObjectAttrEditorPanel = NEW CObjectAttrEditorPanel( _fLeft, _fTop, _fWidth, _fHeight );
    _pObjectAttrEditorPanel->Init();
    _pRootWnd->AddElement( _pObjectAttrEditorPanel );
    m_pObjectAttrEditorPanel = _pObjectAttrEditorPanel;

    _fLeft = 2.0f;
    _fTop = nHeight - 31;
    _fWidth = nWidth - 4;
    _fHeight = 28;
    CStatusBar* _pStatusBar = NEW CStatusBar( _fLeft, _fTop, _fWidth, _fHeight );
    _pStatusBar->Init();
    _pRootWnd->AddElement( _pStatusBar );
    m_pStatusBar = _pStatusBar;

    NOTIFICATION_REGISTERFRONT( NEWGAME, 0, this, CBDGameEditorApp::OnNotification_NewGame_Event );
    NOTIFICATION_REGISTERFRONT( SAVEGAME, 0, this, CBDGameEditorApp::OnNotification_SaveGame_Event );
    NOTIFICATION_REGISTERFRONT( OPENGAME, 0, this, CBDGameEditorApp::OnNotification_OpenGame_Event );


    SNEWGAMEINFO _sNewGameInfo;
    strcpy( _sNewGameInfo.m_strNameSpace, "bdwar" );
    _sNewGameInfo.m_fBlockSize = 1024.0f;
    _sNewGameInfo.m_nBlockCountX = 5;
    _sNewGameInfo.m_nBlockCountY = 5;
    _sNewGameInfo.m_fObjectUnitSize = 32.0f;
    _sNewGameInfo.m_pSceneMgr = NULL;
    NOTIFICATION_SEND( NEWGAME, 0, 0, &_sNewGameInfo );
    return _pRootWnd;
}
void CBDGameEditorApp::OnNotification_NewGame_Event( int32 w, int32 l, void* pData ){
    m_pWorkCanvas->ReleaseMap();
    SNEWGAMEINFO* _pNewGameInfo = (SNEWGAMEINFO*)pData;
    m_pWorkCanvas->NewMap( _pNewGameInfo->m_strNameSpace, 
                          _pNewGameInfo->m_nBlockCountX, 
                          _pNewGameInfo->m_nBlockCountY, 
                          _pNewGameInfo->m_fBlockSize,
                          _pNewGameInfo->m_fObjectUnitSize, "" );
    _pNewGameInfo->m_pSceneMgr = m_pWorkCanvas->GetSceneMgr();
}
void CBDGameEditorApp::OnNotification_SaveGame_Event( int32 w, int32 l, void* pData ){
    SSAVEGAMEINFO* _pSaveGameInfo = (SSAVEGAMEINFO*)pData;
    m_pWorkCanvas->SaveMap( _pSaveGameInfo->m_strFileName );
}
void CBDGameEditorApp::OnNotification_OpenGame_Event( int32 w, int32 l, void* pData ){
    SOPENGAMEINFO* _pOpenGameInfo = (SOPENGAMEINFO*)pData;
    char _strBuff[256];
    int32 _nFileSize;
    void* _pFileData = GetAssetFileData( Format( _strBuff, 256, "%s/maps/%s", g_strResourceWorkPath, _pOpenGameInfo->m_strFileName ), &_nFileSize, NULL );
    if( _pFileData ){
        const char* _strTextContent = (const char*)_pFileData;
        CXML _sXML( _strTextContent );
        m_pWorkCanvas->ReleaseMap();
        m_pWorkCanvas->OpenMap( &_sXML );
        FREE( _pFileData );

        _pOpenGameInfo->m_pSceneMgr = m_pWorkCanvas->GetSceneMgr();
        strcpy( _pOpenGameInfo->m_strNameSpace, _pOpenGameInfo->m_pSceneMgr->GetResourceSpace() );
        CTerrainObject* _pTerrainObject = _pOpenGameInfo->m_pSceneMgr->GetTerrainObject();
        _pOpenGameInfo->m_nBlockCountX = _pTerrainObject->m_nBlockCountX;
        _pOpenGameInfo->m_nBlockCountY = _pTerrainObject->m_nBlockCountY;
        _pOpenGameInfo->m_fBlockSize = _pTerrainObject->m_fBlockSize;
        _pOpenGameInfo->m_fObjectUnitSize = 32;
    }
}
int32 CBDGameEditorApp::Update( int32 nDelta, float fDelta ){
    m_pWorkPanel->Update( nDelta, fDelta );
    m_pWorkCanvas->Update( nDelta, fDelta );
    return CAppLite::Update( nDelta, fDelta );
}
int32 CBDGameEditorApp::Render( int32 nDelta, float fDelta ){
    m_pWorkPanel->Render();
    m_pWorkCanvas->Render( );
    return CAppLite::Render( nDelta, fDelta );
}
int32 CBDGameEditorApp::Final(){
    m_pWorkCanvas->ReleaseMap();
    return CAppLite::Final();
}